<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册界面</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="./js/ajax.js"></script>
    <script src="./js/crypto-js.js"></script>
</head>
<body>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">用户注册</h3>
            </div>
            <div class="panel-body">
                <div class="row content">
                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                        <div class="form-group">
                            <label for="">用户编码</label>
                            <input type="text" class="form-control" id="id" placeholder="请输入用户编码">
                        </div>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                        <div class="form-group">
                            <label for="">用户密码</label>
                            <input type="password" class="form-control" id="password" placeholder="请输入用户密码">
                        </div>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                        <div class="form-group">
                            <label for="">用户姓名</label>
                            <input type="text" class="form-control" id="name" placeholder="请输入用户姓名">
                        </div>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                        <div class="form-group">
                            <label for="">用户年龄</label>
                            <input type="number" class="form-control" id="age" placeholder="请输入用户年龄">
                        </div>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                        <div class="form-group">
                            <label for="">用户性别</label>
                            <input type="text" class="form-control" id="sex" placeholder="请输入用户性别">
                        </div>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                        <div class="form-group">
                            <label for="">电话号码</label>
                            <input type="tel" class="form-control" id="phone" placeholder="请输入电话号码">
                        </div>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary" onclick="registerEvt()">注册</button>
                <button type="submit" class="btn btn-primary" onclick="toLogin()">返回登录界面</button>
            </div>
        </div>
    </div>
    <script>
        function toLogin() {
            window.location.href = './index.html'
        }

        function registerEvt() {
            const data = {}
            let isOk = false
            const infos = document.querySelectorAll('.content input')
            infos.forEach(item => {
                if (!item.value) {
                    isOk = true
                }
            })
            if (isOk) {
                alert('请填写完整！')
                isOk = false
                return
            }
            infos.forEach(item => {
                data[item.id] = item.value
            })
            ajax({
                method: 'POST',
                url: '/user/register',
                data
            }).then(result => {
                alert(result.message)
            }).catch(err => {
                alert('服务器错误！')
            })
        }
    </script>
</body>
</html>